package com.example.bakend.service.Impl;

import com.example.bakend.mapper.BookMapper;
import com.example.bakend.mapper.CategoryMapper;
import com.example.bakend.pojo.Book;
import com.example.bakend.pojo.Category;
import com.example.bakend.pojo.PageInfo;
import com.example.bakend.service.CategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    BookMapper bookMapper;

    @Override
    public Integer insertCategory(Category category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        return categoryMapper.insertCategory(category);
    }

    @Override
    public Integer deleteCategory(Integer id) {
        bookMapper.deleteBook(id);

        return categoryMapper.deleteCategory(id);
    }

    @Override
    public PageInfo getList(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        PageInfo pageInfo = new PageInfo();
        List<Category> books = categoryMapper.getList();
        Page<Category> page = (Page<Category>) books;
        pageInfo.setTotal((int) page.getTotal());
        pageInfo.setRows(page.getResult());
        return pageInfo;
    }

    @Override
    public List<Category> getAll() {
        return categoryMapper.getAll();
    }
}
